package com.lkm.app_ui.ui.fj;

import android.content.Context;

import com.google.gson.reflect.TypeToken;
import com.lkm.app_ui.AppConst;
import com.lkm.app_ui.help.GsonHelp;
import com.lkm.app_ui.help.TaskHelp;
import com.lkm.comlib.task2.DefaultTaskExecutor;
import com.lkm.frame.task2.ITaskExecutor;
import com.lkm.frame.task2.TaskAdapter;
import com.lkm.frame.task2.TaskManager;
import com.lkm.javatool.StringHelp;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;



public class FjWaitOrder {

    private Map<String, FjOrder> fjOrderMap = new Hashtable<>();

    void init() {
        String json = AppConst.application.getSharedPreferences("fj_waitOrder", Context.MODE_PRIVATE).getString("data", "");

        Map<String, FjOrder> l = GsonHelp.fromJson(json, new TypeToken<Map<String, FjOrder>>() {
                }.getType()
        );
        if (l != null) {

            for (FjOrder or :
                    l.values()) {
                if (or.fjItems != null)
                    for (FjOrder.FjItem item :
                            or.fjItems) {
                        item.fjInfoNo = or.fjInfoNo;
                        item.createTime = or.createTime;
                    }
            }
            fjOrderMap.putAll(l);
        }

    }

    public int getSize() {
        return fjOrderMap.size();
    }

    public Collection<FjOrder> getFjOrders() {
        return fjOrderMap.values();

    }

    public boolean hasOrder(String orderNo) {
        return fjOrderMap.containsKey(orderNo);
    }



    public FjOrder popWaitOrder(String orderNo, FjOrder newFj) {


        if (StringHelp.isEmpty(orderNo)) {

            try {
                orderNo = fjOrderMap.keySet().iterator().next();
            } catch (Exception e) {
            }
        }
        if (orderNo == null) return null;
        FjOrder oldFjOrder = fjOrderMap.remove(orderNo);
        if (oldFjOrder == null) return null;

        if (newFj == null) return oldFjOrder;
        if (oldFjOrder == null) return null;
        if (oldFjOrder.fjItems == null) oldFjOrder.fjItems = new ArrayList<>();
        if (newFj.fjItems == null) newFj.fjItems = new ArrayList<>();

        Map<String, FjOrder.FjItem> fjItemMap = new HashMap<>();
        for (FjOrder.FjItem item : oldFjOrder.fjItems) {
            fjItemMap.put(item.getUnStr(), item);
        }

        for (FjOrder.FjItem item : newFj.fjItems) {
            FjOrder.FjItem old = fjItemMap.get(item.getUnStr());
            if (old != null)
                item.realWeight = old.realWeight;
        }
        return newFj;
    }




    private FjOrder popWaitOrder(String orderNo) {
        FjOrder fjOrder = fjOrderMap.remove(orderNo);

        if (fjOrder != null) {
            syncToLocal();
        }
        return fjOrder;
    }

    public void putWaitOrder(FjOrder fjOrder) {
        fjOrderMap.put(fjOrder.fjInfoNo, fjOrder);
        syncToLocal();
    }




    private void syncToLocal() {
        TaskHelp.cancelById(SaveTask.class.getName());
        SaveTask task = new SaveTask(new DefaultTaskExecutor(AppConst.getTaskExecutor(), true), AppConst.getTaskManager2());
        task.action();
        task.execTask();
    }

    private class SaveTask extends TaskAdapter {
        public SaveTask(ITaskExecutor taskExecutor, TaskManager mTaskManager) {
            super(taskExecutor, SaveTask.class.getName(), mTaskManager);
        }

        @Override
        public void doExecuting(Object param) {
            super.doExecuting(param);
            mTaskExecutor.executor(new Runnable() {
                @Override
                public void run() {
                    if (!getIsContinue()) return;
                    String s = GsonHelp.toJson(fjOrderMap);
                    if (!getIsContinue()) return;

                    AppConst.application.getSharedPreferences("fj_waitOrder", Context.MODE_PRIVATE).edit().putString("data", s).commit();
                }
            });
        }
    }
}
